﻿'
' Created by SharpDevelop.
' User: jdavis
' Date: 9/27/2010
' Time: 8:48 AM
'
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Public Partial Class MainForm
	Dim SeedValue As Integer = now.Year
	
	Public Sub New()
		' The Me.InitializeComponent call is required for Windows Forms designer support.
		Me.InitializeComponent()
		rnd(-1) 'Initializing in a way to be repeatable
		'
		' TODO : Add constructor code after InitializeComponents
		'
	End Sub
	
	
	
	Sub MakeAssignments()
		'Make a list of names
		Dim ValidNames() As  String = Me.tbInputNames.Lines
		dim NewAssignments(validnames.GetLength(0)-1,1) as string
		'make a list of exclusions, adding people giving to themselves
		Dim inputExclusionLines() As  String = Me.tbExclusions.Lines
		Dim ExclusionsA As new  List(Of String)  'NEVER sort these
		dim ExclusionsB as new  List(of string) 'NEVER sort these, so they match
		
		dim UnassignedNames as new list(of string)
		
		
		'Add exclusions for lines from exclusions box
		'splits using "TO")
		If inputexclusionlines.GetLength(0) >0 Then
			dim tempStrings(1) as string
			For i As Integer = 0 To inputexclusionlines.getlength(0)-1
				if inputexclusionlines(i) <> "" then
					tempstrings = inputexclusionlines(i).Split("TO")
					ExclusionsA.Add(tempstrings(0).Trim)
					exclusionsB.Add(mid(tempstrings(1),2).trim)
					end if
				Next
			End If
			
			'Adds exclusions for giving to self
			For i As Integer = 0 To validnames.GetLength(0)-1
				Dim name As  String
				validnames(i) = validnames(i).trim
				name = validnames(i)
				
				exclusionsA.Add(name)
				exclusionsB.Add(name)
			Next
			'****All exclusions are added at this point***
			
			
			
			
			randomize(Seedvalue)
			
			'Try a ridiculously large number of times before giving up
			Dim ResultsValid As Boolean = True
			
			for m as Integer = 0 to 50
				For i As Integer = 0 To 1000
					unassignednames.Clear
					Unassignednames.AddRange(validnames) 'Start with valid names again
					For j As Integer = 0 To validnames.GetLength(0)-1 'Need an assignment for each name
						
						NewAssignments(j,0)=validnames(j)
						
						dim pns as new List(Of string)
						pns.Clear
						pns.AddRange(unassignednames)
						'remove exclusions from possibilities
						pns.Remove(validnames(j))
						
						For k As Integer = 0 To ExclusionsA.Count - 1
							If ExclusionsA(k) = validnames(j) Then
								pns.Remove(ExclusionsB(k))
							End If
						Next
						If pns.Count = 0 Then
							resultsvalid = False
							exit for
						End If
						newassignments(j,1)=pns(int(rnd()*pns.Count))
						'remove assigned name from potential candidates
						unassignednames.Remove(newassignments(j,1))
					Next j
					
					Dim nns As new List(Of String)
					nns.Clear
					For j As Integer = 0 To newassignments.GetLength(0)-1
						nns.Add(newassignments(j,1))
					Next
					nns.Sort
					For j As Integer = 1 To nns.Count -1
						if nns(j) = nns(j-1) then NewAssignments(0,0) = "INVALID"
					Next
					'Need to add condition for exiting for i loop
					If i = 1000 Then
						NewAssignments(0,0) = "INVALID"
					End If
					if resultsvalid then exit for 'Exits for i
				Next
				if newassignments(0,0) <>  "INVALID" then exit for
			next
			
			Dim nastring As String = ""
			
			For i As Integer = 0 To validnames.GetLength(0)-1
				nastring += NewAssignments(i,0) +" TO " + NewAssignments(i,1) + vbnewline
			Next
			If newassignments(0,0) =  "INVALID" Then 
				nastring = "INVALID: Try again"
				Seedvalue = cint(rnd()*10000+1)
			End If
			tbNewAssignments.Text = nastring
			
		end sub
		'for each name
		'make a list of possibilities, remove exclusions
		'use random number to make assignment
		
		
		
		
		
		Sub BtnMakeAssignmentsClick(sender As Object, e As EventArgs)
			makeassignments()
		End Sub
	End Class
